package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQuery;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;


    @RequestMapping("/list")
    @RequiredPermission(name="员工页面",expression="employee:list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQuery qo){
        Page<Employee> pageResult = employeeService.queryPage(qo);
        model.addAttribute("Page",pageResult);
        model.addAttribute("departments",departmentService.list());
        return "employee/list";
    }
    @RequiredPermission(name="员工删除",expression="employee:delete")
    @RequestMapping("/delete")
    public String delete(Long id ){
        employeeService.removeById(id);
        return "redirect:/employee/list";
    }

    //去到新增页面
    @RequiredPermission(name="员工新增/编辑",expression="employee:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id ,Model model ){
        //把部门数据查出来
        model.addAttribute("role",roleService.list());
        model.addAttribute("departments",departmentService.list());
        if(id != null){
            model.addAttribute("roleSelected",roleService.selectEmpAndRole(id));
            model.addAttribute("employee",employeeService.selectByPrimaryKey(id));
        }
        return  "/employee/input";
    }

    /*@RequiredPermission(name="员工新增/编辑",expression="employee:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public MyAJAX saveOrUpdate(Employee employee, Long[] ids){
        if(employee.getId() != null){
            employeeService.update(employee,ids);
        }else {
            employeeService.insert(employee,ids);
        }
        return new MyAJAX();
    }*/

}
